﻿Public Class FormPrez

    Private ClipDiapo As ClassDiapo = Nothing

    Public Sub Clear()
        Me.ImageList2.Images.Clear()
        Me.ListView1.Items.Clear()
        Me.ListViewCalques.Items.Clear()
        Me.ClipDiapo = Nothing
        Me.Refresh()
    End Sub

    Private Sub ListView1_SelectedIndexChanged(sender As System.Object, e As System.EventArgs) Handles ListView1.SelectedIndexChanged
        If Me.ListView1.SelectedItems.Count > 0 Then
            Dim oDiapoActive As ClassDiapo = Me.ListView1.SelectedItems(0).Tag
            Me.TextBoxTitreDiapo.Text = oDiapoActive.Titre
            'Récupérer les calques
            Me.ListViewCalques.Items.Clear()
            Dim oPage As Microsoft.Office.Interop.Visio.Page = Nothing
            Try
                oPage = oApp.ActiveDocument.Pages.Item(oDiapoActive.PageName)
                Me.CheckBoxShowRulers.Checked = (oDiapoActive.ShowRulers = -1)
                Me.CheckBoxShowScrollBar.Checked = (oDiapoActive.ShowScrollBars = 5)
                Me.CheckBoxShowGrid.Checked = (oDiapoActive.ShowGrid = -1)
                Me.CheckBoxShowPageTab.Checked = (oDiapoActive.ShowPageTab = -1)
                Me.LabelGauche.Text = oDiapoActive.vLeft.ToString
                Me.Labelhaut.Text = oDiapoActive.vTop.ToString
                Me.LabelLargeur.Text = oDiapoActive.vWidth.ToString
                Me.LabelHauteur.Text = oDiapoActive.vHeight.ToString
                'Calques définis de la page courante
                For Each oLayer As Microsoft.Office.Interop.Visio.Layer In oPage.Layers
                    Dim oCalque As ClassCalque
                    If oDiapoActive.Calques.Contains(oLayer.Name) Then
                        oCalque = oDiapoActive.Calques(oLayer.Name)
                    Else
                        oCalque = New ClassCalque
                        oCalque.Nom = oLayer.Name
                        oCalque.Visible = oLayer.CellsC(visLayerVisible).FormulaU <> "0"
                        oDiapoActive.Calques.add(oCalque)
                    End If
                    Dim oItem As Windows.Forms.ListViewItem = Me.ListViewCalques.Items.Add(oCalque.Nom, oCalque.Nom, "calque")
                    oItem.Checked = oCalque.Visible
                    oItem.Tag = oCalque
                Next
            Catch Ex As Exception
                'Finalement on fait rien
            End Try
            'Afficher
            oDiapoActive.Appliquer()
            UpdateRibbon()
        End If
    End Sub

    Public Sub UpdateRibbon()
        Dim iIndex As Integer = -1
        If Me.ListView1.SelectedIndices.Count > 0 Then iIndex = Me.ListView1.SelectedIndices(0)
        Globals.Ribbons.RibbonPrez.LabelDiapo.Label = iIndex + 1 & "/" & Me.ListView1.Items.Count
        If iIndex <> -1 Then Globals.Ribbons.RibbonPrez.LabelTitre.Label = ListView1.Items(iIndex).Text
        Dim bDiapos As Boolean = (Me.ListView1.Items.Count > 0)
        Globals.Ribbons.RibbonPrez.ButtonDebut.Enabled = bDiapos
        Globals.Ribbons.RibbonPrez.ButtonFin.Enabled = bDiapos
        Globals.Ribbons.RibbonPrez.ButtonSuiv.Enabled = bDiapos
        Globals.Ribbons.RibbonPrez.ButtonPrec.Enabled = bDiapos
        Globals.Ribbons.RibbonPrez.ButtonExport.Enabled = bDiapos
        Globals.Ribbons.RibbonPrez.ButtonSupprimer.Enabled = bDiapos
        Globals.Ribbons.RibbonPrez.Button1.Enabled = bDiapos
        Globals.Ribbons.RibbonPrez.Button2.Enabled = bDiapos
        Globals.Ribbons.RibbonPrez.PerformLayout()
        Windows.Forms.Application.DoEvents()
    End Sub

    Private Sub FormPrez_Resize(sender As Object, e As System.EventArgs) Handles Me.Resize
        Me.ListView1.Columns(0).Width = Me.ListView1.Width - 100
    End Sub

    Private Sub ButtonOk_Click(sender As System.Object, e As System.EventArgs) Handles ButtonOk.Click
        Me.DialogResult = Windows.Forms.DialogResult.OK
        Me.Close()
    End Sub

    Private Sub ListView1_DragDrop(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles ListView1.DragDrop
        If ListView1.SelectedItems.Count = 0 Then Exit Sub
        Dim p As System.Drawing.Point = ListView1.PointToClient(New System.Drawing.Point(e.X, e.Y))
        Dim dragToItem As System.Windows.Forms.ListViewItem = ListView1.GetItemAt(p.X, p.Y)
        If dragToItem Is Nothing Then Exit Sub
        Dim dragIndex As Integer = dragToItem.Index
        Dim i As Integer
        Dim sel(ListView1.SelectedItems.Count) As System.Windows.Forms.ListViewItem
        For i = 0 To ListView1.SelectedItems.Count - 1
            sel(i) = ListView1.SelectedItems.Item(i)
        Next
        For i = 0 To ListView1.SelectedItems.Count - 1
            Dim dragItem As System.Windows.Forms.ListViewItem = sel(i)
            Dim itemIndex As Integer = dragIndex
            If itemIndex = dragItem.Index Then Exit Sub
            If dragItem.Index < itemIndex Then
                itemIndex = itemIndex + 1
            Else
                itemIndex = dragIndex + i
            End If
            Dim insertItem As System.Windows.Forms.ListViewItem = CType(dragItem.Clone, System.Windows.Forms.ListViewItem)
            ListView1.Items.Insert(itemIndex, insertItem)
            ListView1.Items.Remove(dragItem)
        Next
    End Sub

    Private Sub ListView1_DragEnter(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles ListView1.DragEnter
        Dim i As Integer
        For i = 0 To e.Data.GetFormats().Length - 1
            If e.Data.GetFormats()(i).Equals("System.Windows.Forms.ListView+SelectedListViewItemCollection") Then
                e.Effect = System.Windows.Forms.DragDropEffects.Move
            End If
        Next
    End Sub

    Private Sub ListView1_ItemDrag(ByVal sender As Object, ByVal e As System.Windows.Forms.ItemDragEventArgs) Handles ListView1.ItemDrag
        'Initier le drag de l'item courant avec un move
        ListView1.DoDragDrop(ListView1.SelectedItems, System.Windows.Forms.DragDropEffects.Move)
    End Sub

    Private Sub SupprimerCourant()
        If Me.ListView1.SelectedItems.Count > 0 Then
            If Not Me.ClipDiapo Is Nothing Then
                If Me.ClipDiapo.ID = Me.ListView1.SelectedItems(0).Tag.Id Then Me.ClipDiapo = Nothing
            End If
            Me.ImageList2.Images.RemoveByKey(Me.ListView1.SelectedItems(0).ImageKey)
            Me.ListView1.SelectedItems(0).Tag = Nothing
            Me.ListView1.SelectedItems(0).Remove()
        End If
    End Sub

    Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
        ToutSupprimer()
    End Sub

    Private Sub ToutSupprimer()
        If MsgBox("Delete all slides?", vbOKCancel Or vbQuestion) = vbOK Then
            Me.Clear()
        End If
    End Sub

    Private Sub Button2_Click(sender As System.Object, e As System.EventArgs) Handles Button2.Click
        SupprimerCourant()
    End Sub

    Private Sub TextBoxTitreDiapo_TextChanged(sender As System.Object, e As System.EventArgs) Handles TextBoxTitreDiapo.TextChanged
        If Me.ListView1.SelectedItems.Count > 0 Then
            Me.ListView1.SelectedItems(0).Text = Me.TextBoxTitreDiapo.Text
            Me.ListView1.SelectedItems(0).Tag.Titre = Me.TextBoxTitreDiapo.Text
        End If
    End Sub

    Private Sub ListViewCalques_ItemChecked(sender As Object, e As System.Windows.Forms.ItemCheckedEventArgs) Handles ListViewCalques.ItemChecked
        Dim oCalque As ClassCalque = e.Item.Tag
        If oCalque IsNot Nothing Then
            oCalque.Visible = e.Item.Checked
            Try
                Dim oDiapoActive As ClassDiapo = Me.ListView1.SelectedItems(0).Tag
                Dim oPage As Microsoft.Office.Interop.Visio.Page = oApp.ActiveDocument.Pages.Item(oDiapoActive.PageName)
                Dim oLayer As Microsoft.Office.Interop.Visio.Layer = oPage.Layers(oCalque.Nom)
                If oCalque.Visible Then
                    oLayer.CellsC(visLayerVisible).FormulaU = -1
                Else
                    oLayer.CellsC(visLayerVisible).FormulaU = 0
                End If
            Catch Ex As Exception
                'Rien
            End Try
        End If
    End Sub

    Private Sub CheckBoxShowRulers_CheckedChanged(sender As System.Object, e As System.EventArgs) Handles CheckBoxShowRulers.CheckedChanged
        Try
            Dim oDiapoActive As ClassDiapo = Me.ListView1.SelectedItems(0).Tag
            oDiapoActive.ShowRulers = CShort(Me.CheckBoxShowRulers.Checked)
            If oApp.ActiveWindow IsNot Nothing Then
                oApp.ActiveWindow.ShowRulers = oDiapoActive.ShowRulers
            End If
        Catch Ex As Exception
            'Rien
        End Try
    End Sub

    Private Sub CheckBoxShowScrollBar_CheckedChanged(sender As System.Object, e As System.EventArgs) Handles CheckBoxShowScrollBar.CheckedChanged
        Try
            Dim oDiapoActive As ClassDiapo = Me.ListView1.SelectedItems(0).Tag
            If Me.CheckBoxShowScrollBar.Checked Then
                oDiapoActive.ShowScrollBars = 5
            Else
                oDiapoActive.ShowScrollBars = 0
            End If
            If oApp.ActiveWindow IsNot Nothing Then
                oApp.ActiveWindow.ShowScrollBars = oDiapoActive.ShowScrollBars
            End If
        Catch Ex As Exception
            'Rien
        End Try
    End Sub

    Private Sub CheckBoxShowGrid_CheckedChanged(sender As System.Object, e As System.EventArgs) Handles CheckBoxShowGrid.CheckedChanged
        Try
            Dim oDiapoActive As ClassDiapo = Me.ListView1.SelectedItems(0).Tag
            oDiapoActive.ShowGrid = CInt(Me.CheckBoxShowGrid.Checked)
            If oApp.ActiveWindow IsNot Nothing Then
                oApp.ActiveWindow.ShowGrid = oDiapoActive.ShowGrid
            End If
        Catch Ex As Exception
            'Rien
        End Try
    End Sub

    Private Sub CheckBoxShowPageTab_CheckedChanged(sender As System.Object, e As System.EventArgs) Handles CheckBoxShowPageTab.CheckedChanged
        Try
            Dim oDiapoActive As ClassDiapo = Me.ListView1.SelectedItems(0).Tag
            oDiapoActive.ShowPageTab = CInt(Me.CheckBoxShowPageTab.Checked)
            If oApp.ActiveWindow IsNot Nothing Then
                oApp.ActiveWindow.ShowPageTabs = oDiapoActive.ShowPageTab
            End If
        Catch Ex As Exception
            'Rien
        End Try
    End Sub

    Private Sub Button3_Click(sender As System.Object, e As System.EventArgs) Handles Button3.Click
        If Me.ListView1.SelectedItems.Count > 0 Then
            Me.ClipDiapo = Me.ListView1.SelectedItems(0).Tag
        End If
    End Sub

    Private Sub Button4_Click(sender As System.Object, e As System.EventArgs) Handles Button4.Click
        If (Me.ListView1.SelectedItems.Count > 0) And (Not Me.ClipDiapo Is Nothing) Then
            If MsgBox("Make this slide's view similar to " & ClipDiapo.Titre & "?", MsgBoxStyle.OkCancel) = vbOK Then
                Dim oDiapo As ClassDiapo = Me.ListView1.SelectedItems(0).Tag
                oDiapo.vTop = ClipDiapo.vTop
                oDiapo.vLeft = ClipDiapo.vLeft
                oDiapo.vHeight = ClipDiapo.vHeight
                oDiapo.vWidth = ClipDiapo.vWidth
                Me.LabelGauche.Text = oDiapo.vLeft.ToString
                Me.Labelhaut.Text = oDiapo.vTop.ToString
                Me.LabelLargeur.Text = oDiapo.vWidth.ToString
                Me.LabelHauteur.Text = oDiapo.vHeight.ToString
                oDiapo.Appliquer()
            End If
        Else
            MsgBox("You must first copy a slide view coordinates!")
        End If
    End Sub
End Class